package com.csh.szhm.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.csh.szhm.R;
import com.csh.szhm.http.HttpRequestTool;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.module.city.SzhmCityListBean;
import com.csh.szhm.module.city.SzhmCityListItemBean;
import com.csh.szhm.utils.DebugUntil;
import com.csh.szhm.utils.sharepreferences.SharedPreferencesUtil;
import com.google.gson.reflect.TypeToken;
import com.liucanwen.citylist.adapter.CityAdapter;
import com.liucanwen.citylist.data.CityData;
import com.liucanwen.citylist.model.CityItem;
import com.liucanwen.citylist.widget.ContactItemInterface;
import com.liucanwen.citylist.widget.ContactListViewImpl;

public class SzhmSelectCityActivity extends MyBaseActivity<SzhmCityListBean>
		implements TextWatcher {

	private ContactListViewImpl listview;

	private EditText searchBox;
	private String searchString;

	private Object searchLock = new Object();
	boolean inSearchMode = false;

	List<ContactItemInterface> contactList;
	List<ContactItemInterface> filterList;
	private SearchListTask curSearchTask = null;

	@Override
	public void init() {
		requestNet();
		initContentView(R.layout.szhm_select_city_layout);
		setMyTitle("城市列表");
		listview = (ContactListViewImpl) this.findViewById(R.id.listview);

		listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				List<ContactItemInterface> searchList = inSearchMode ? filterList
						: contactList;
				SharedPreferencesUtil.getInstance().setCityName(
						searchList.get(position).getDisplayInfo());
				SharedPreferencesUtil.getInstance().setCityId(
						searchList.get(position).getItemId());
				dissmisCallChild();
			}
		});
		searchBox = (EditText) findViewById(R.id.input_search_query);
		searchBox.addTextChangedListener(this);
	}

	@Override
	protected void requestNet() {
		if (mHttpRequestTool == null) {
			mHttpRequestTool = new HttpRequestTool<SzhmCityListBean>();
			mTypeToken = new TypeToken<SzhmCityListBean>() {
			};
			mHttpRequestTool.setHttpCallBackUi(this);
		}
		mHttpRequestTool.cloneRequest(Method.GET, HttpUrls.mCitySelect,
				mTypeToken, getClass().getSimpleName(), "CITY");
		super.requestNet();
	}

	@Override
	public void success(SzhmCityListBean data) {
		if (data != null) {
			List<SzhmCityListItemBean> list = data.getList();
			if (list != null && list.size() > 0) {
				filterList = new ArrayList<ContactItemInterface>();
				contactList = CityData.getSampleContactList(list);
				CityAdapter adapter = new CityAdapter(this, R.layout.city_item,
						contactList);
				listview.setFastScrollEnabled(false);
				listview.setAdapter(adapter);
			} else {
				DebugUntil.createInstance().toastStr("无可用数据！");
			}
		}
		super.success(data);
	}

	@Override
	public void afterTextChanged(Editable s) {
		searchString = searchBox.getText().toString().trim().toUpperCase();
		if (curSearchTask != null
				&& curSearchTask.getStatus() != AsyncTask.Status.FINISHED) {
			try {
				curSearchTask.cancel(true);
			} catch (Exception e) {
			}
		}
		curSearchTask = new SearchListTask();
		curSearchTask.execute(searchString);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

	private class SearchListTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... params) {
			filterList.clear();
			String keyword = params[0];
			inSearchMode = (keyword.length() > 0);
			if (inSearchMode) {
				// get all the items matching this
				for (ContactItemInterface item : contactList) {
					CityItem contact = (CityItem) item;

					boolean isPinyin = contact.getFullName().toUpperCase()
							.indexOf(keyword) > -1;
					boolean isChinese = contact.getNickName().indexOf(keyword) > -1;
					if (isPinyin || isChinese) {
						filterList.add(item);
					}
				}
			}
			return null;
		}

		protected void onPostExecute(String result) {
			synchronized (searchLock) {
				if (inSearchMode) {
					CityAdapter adapter = new CityAdapter(
							SzhmSelectCityActivity.this, R.layout.city_item,
							filterList);
					adapter.setInSearchMode(true);
					listview.setInSearchMode(true);
					listview.setAdapter(adapter);
				} else {
					CityAdapter adapter = new CityAdapter(
							SzhmSelectCityActivity.this, R.layout.city_item,
							contactList);
					adapter.setInSearchMode(false);
					listview.setInSearchMode(false);
					listview.setAdapter(adapter);
				}
			}
		}
	}

	@Override
	public void onBackPressed() {
		String txt = searchBox.getText().toString();
		if (txt == null || txt.equals("")) {
			//
			super.onBackPressed();
		} else {
			searchBox.setText("");
		}
	}

}
